﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
		
	/** The highest level container for this project, a task wraps all items. */
	public class Task extends MovieClip
	{
		// private variables
		private var buildMode:Boolean;
		private var currentSection:int=0;
		
		// private objects
		private var xml:XML;
		private var sectionsxml:XMLList;
		private var section:Section;
		
		public function Task(xml:XML, buildMode:Boolean=false)
		{
			this.xml = xml;
			this.buildMode = buildMode;
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded (event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			this.graphics.drawRect(0,0,this.parent.width, this.parent.height);
			
			sectionsxml = xml..section;
			newSection();
		}
		/** Open a new section */
		private function newSection():void
		{
			if (currentSection < sectionsxml.length())
			{
				var isFinalFinal:Boolean = false;
				if (currentSection == sectionsxml.length()-1) isFinalFinal = true;
				section = new Section(sectionsxml[currentSection], isFinalFinal, buildMode);
				section.addEventListener(Event.COMPLETE, handleSectionComplete);
				this.addChild(section);
			} else
			{
				this.dispatchEvent(new Event(Event.COMPLETE));				
			}			
		}
		/** The section is complete, iterate and try another */
		private function handleSectionComplete(event:Event):void
		{
			section.removeEventListener(Event.COMPLETE, handleSectionComplete);
			this.removeChild(section);
			currentSection++;
			newSection();
		}
	}
}